<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0,
		minimum-scale=1.0, maximum-scale=1.0"/>
    <meta charset="UTF-8">
    <title>申请退货</title>
    <link href="../public/css/style.css" rel="stylesheet">
    <link href="../public/css/common.css" rel="stylesheet">
    <script type="text/javascript" src="../public/js/jquery.js" ></script>
    <script type="text/javascript" src="../public/js/customer.js" ></script>
    <script src="../public/js/product.js"></script>
</head>
<body class="layout_body">
<div class="layout_content graybg"  style="padding-bottom: 0;">
    <header class="demo-header" style="padding-top: 10px;">
        <div class="titleLeft flt">
            <a href="">
                <img src="../public/img/back.png" width="35px" height="33px">
            </a>
        </div>
        <div class="titleRight flt text-center">申请退货</div>
    </header>
    <div class="whitebg">
        <div class="sizecolor">商品信息</div>
        <div class="message-detail border1 text-right fontsize zhemanjian" id="return-detail-prolist" style="padding-right: 1%;">
            <div class="product-list newproduct" style="padding-bottom: 0;border: none;">
                <?php if (!empty($detail['orderBody'])){ ?>
                    <?php foreach ($detail['orderBody'] as $key => $orderbody){ ?>
                        <div class="product-list-div" style="padding-bottom: 15px;">
                            <img src="<?php echo $orderbody['img']?>"/>
                            <div class="flt pline">
                                <p class="product"><?php echo $orderbody['product_name']?></p>
                                <p class="guige"><?php echo $orderbody['mass_packing']?></p>
                                <p class="product">￥<?php echo $orderbody['price']?></p>
                            </div>
                            <div class="flt cart returnD_number">
                                <div class="text-center guige">购买数量：<?php echo $orderbody['amount']?></div>
                                <div class="flt text-right appl-jiajian">
                                    <div class="inputNum" style="margin-bottom:5px;">
                                        <span class="minus minadd" onclick="javascript:minus_(this);return false;" amount="<?php echo $orderbody['amount']; ?>" price="<?php echo $orderbody['price']; ?>" >-</span>
                                        <span class="proNum" >
                                                <input class="input_val"  type="text" product_name="<?php echo $orderbody['product_name']?>" amount="<?php echo $orderbody['amount']; ?>"  skuid="<?php echo $orderbody['product_sku_id']; ?>"  pid="<?php echo $orderbody['product_id']; ?>" price="<?php echo $orderbody['price']; ?>"   value="0" onblur="javascript:checkInt(this);return false;">
                                            </span>
                                        <span class="add minadd" onclick="javascript:add(this);return false;" amount="<?php echo $orderbody['amount']; ?>"  price ="<?php echo $orderbody['price']; ?>" >+</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php } ?>
                <?php } ?>

            </div>
        </div>
    </div>
    <div class="whitebg">
        <div class="sizecolor">商品汇总</div>
        <div class="border1">
            <div class="padding-top product flt">商品总额</div><div class="frt zonge">￥<?php echo $detail['orderHeader']['price']?></div>
            <div class="clear_both"></div>
            <div class="padding-top product flt">退款总额</div><div class="frt Cgray button-two refund_price">0.00</div>
            <div class="clear_both"></div>
            <div class="padding-top product"><div class="beizhu">备注信息</div><textarea placeholder="请输入备注信息" class="beizhu-area"></textarea></div>
        </div>
    </div>
    <div class="whitebg">
        <div class="sizecolor">上传图片<span class="Cgray fontsize" style="font-weight: normal;padding-left: 2%;">(最多上传10张图片)</span></div>
        <div class="ordetail-img return-order-img">
            <div class="uploadImg">
                <label class="result"><input type="file">+</label>
            </div>
        </div>
    </div>
    <div class="whitebg">
        <input type="hidden" class="orderid" value="<?php echo $detail['orderHeader']['id']; ?>" />
        <div class="padding-adjust4"><div class="button-two Cgray refund">提交退货</div>
        </div>
    </div>
    <script type="text/javascript">
        $('body').on('click','.refund',function (el) {
            var total = $('.refund_price').text(); //退单总价
            var remark = $('.beizhu-area').val(); //备注
            var orderid = $('.orderid').val(); //订单ID
            var product_info = [];
            var count_amount = 0;
            var img_val =[];
            $('.img_val').each(function(index,el){
                var val = $(el).attr("src");
                var oneRow = [];
                oneRow.push(val);
                var one = oneRow.join();
                img_val.push(one);

            });
            $('.input_val').each(function(index,el){
                var price = parseInt($(el).attr('price')); // 单价
                var skuid = $(el).attr('skuid'); // skuid
                var product_name = $(el).attr('product_name'); // 商品名称
                var practical_amount = $(el).attr('amount'); // 退货实际数量
                var pid = $(el).attr('pid'); // 商品ID
                var amount = $(el).val(); //数量
                count_amount +=amount; //统计退货数量的变化
                var oneItem = [];
                oneItem.push(skuid);
                oneItem.push(pid);
                oneItem.push(amount);
                oneItem.push(price);
                product_info.push(oneItem.join(':'))
            });
            if (count_amount <= 0){
                layer.msg('请选择退货的商品!');return;
            }
            var detail = '';
            for (var i=0;i<product_info.length;i++){
                detail += product_info[i]+';';
            }
            detail = detail.substr(0, detail.length - 1);
            var index = layer.msg('正在退货中', {icon: 16,shade: 0.01,time:1000000});
            $.ajax({
                url: '/ajax/orderfrom/',
                type: 'post',
                dataType: 'json',
                data: {
                    'total': total,
                    'remark': remark,
                    'orderid': orderid,
                    'detail': detail,
                    'img_val':img_val,
                    'ajaxdata': 'refund'
                },
                success: function (response) {
                    layer.msg(response.msg);
                    window.location.href ='/orderfrom/index';
                },
                beforeSend: function () {
                }
            });
        });
        /*
         * 输入框输入退款数量
         *
         */
        function checkInt() {
            var total = 0;
            $('.input_val').each(function(index,el){
                var price = parseInt($(el).attr('price')); //商品价格
                var amount = parseInt($(el).attr('amount')); //实际商品数量
                var inputVal = $(el).val();//退货商品数量
                if (!isInteger(inputVal)){  //判断输入的退款数量是否是整数
                    layer.msg('请输入整数!');
                    $(el).val(0);
                    return;
                }
                if (inputVal<0){  //判断输入的退款数量是否是整数
                    layer.msg('请输入整数!');
                    $(el).val(0);
                    return;
                }
                if (inputVal >amount){
                    $(el).val(0);
                    layer.msg('退款数量不能大于商品购买数量');return;
                }
                total += inputVal * price; //重新计算退单总金额
            });
            if (total ><?php echo $detail['orderHeader']['price'] ?>){
                layer.msg('退款总金额不能超过商品总额');return;
            }
            $('.refund_price').text(total);
        }
        /*
         * 减退款数量
         */
        function minus_(el) {
            var inputVal = $(el).next().find('input').val();
            var productId =$(el).attr('pid');
            if(inputVal<=0){
                return false;
            }
            var newVal = parseInt(inputVal)-1;
            $(el).next().find('input').val(newVal);

            var price = parseInt($(el).attr('price'));
            var total_odl_price = parseInt($('.refund_price').text());
            var total = total_odl_price - price;
            $('.refund_price').text(total);
        }
        /*
         * 加退款数量
         */
        function add(el){
            var price = parseInt($(el).attr('price'));
            var amount = parseInt($(el).attr('amount'));
            var total_odl_price = parseInt($('.refund_price').text());
            var total = total_odl_price + price;
            var inputVal = $(el).prev().find('input').val();
            var newVal = parseInt(inputVal)+1;
            if (newVal>amount){
                layer.msg('退款数量不能大于商品购买数量!');return;
            }
            $(el).prev().find('input').val(newVal);
            $('.refund_price').text(total);
        }
        /*
         * 判断是否是整数
         */
        function isInteger( str ){
            var regu = /^[-]{0,1}[0-9]{1,}$/;
            return regu.test(str);
        }
        window.onload = function()
        {
            imgUpload();
        }
    </script>
</body>
</html>
